package com.framework.bdf4j.msg.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.framework.bdf4j.msg.constant.PublicConstant;
import com.framework.bdf4j.msg.entity.domain.SysMsgReceiver;
import com.framework.bdf4j.msg.entity.dto.SysMsgRecieverSaveDTO;
import com.framework.bdf4j.msg.mapper.SysMsgReceiverMapper;
import com.framework.bdf4j.msg.service.SysMsgReceiverService;
import com.framework.bdf4j.msg.util.DtoEntityUtil;
import com.framework.bdf4j.sysadm.entity.SysUser;
import com.framework.bdf4j.sysadm.service.SysUserService;
import com.framework.bdf4j.sysadm.vo.SecurityUser;

/**
 * 消息发送服务
 *
 * @author shan.deng
 * @date 2020-07-15 16:06:43
 */
@Service
public class SysMsgRecieverServiceImpl extends ServiceImpl<SysMsgReceiverMapper, SysMsgReceiver> implements SysMsgReceiverService {

    @Resource
    private SysUserService sysUserService;

    /**
     * 查询未读的消息
     * @return
     */
    @Override
    public Integer countNotReadMessage() {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            SecurityUser scauser = (SecurityUser)authentication.getPrincipal();

            return getBaseMapper().countReceiverMessage(scauser.getUserId());
        }
        return 0;
    }

    /**
     * 将消息置为已读
     * @param id
     */
    @Override
    public Boolean readMessageById(Integer id) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            SecurityUser scauser = (SecurityUser)authentication.getPrincipal();
            Integer count = getBaseMapper().updateReceiverMessageRead(scauser.getUserId(),id);
            if(count > 0) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean saveReceiverBatch(Integer msgId, List<SysMsgRecieverSaveDTO> sysMsgRecieverSaveDTOS) {
        List<SysMsgReceiver> sysMsgReceivers = DtoEntityUtil.trans(sysMsgRecieverSaveDTOS, SysMsgReceiver.class);
        sysMsgReceivers.stream().forEach(sysMsgReceiver -> {
            sysMsgReceiver.setMsgId(msgId);
            sysMsgReceiver.setReceiverType(PublicConstant.RECEIVER_TYPE_PERSON);
            save(sysMsgReceiver);
        });
        return true;
    }

    @Override
    public Boolean saveAllPerson(Integer msgId) {
        List<Integer> ids = sysUserService.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getDelFlag, "0")).stream().map(sysUser -> sysUser.getUserId()).collect(Collectors.toList());
        List<SysMsgReceiver> list = new ArrayList<>();
        ids.stream().forEach(id -> {
            SysMsgReceiver sysMsgReceiver = new SysMsgReceiver();
            sysMsgReceiver.setMsgId(msgId);
            sysMsgReceiver.setReceiverType(PublicConstant.RECEIVER_TYPE_GLOBAL);
            sysMsgReceiver.setReceiverUserId(id);
            save(sysMsgReceiver);
//            list.add(sysMsgReceiver);
        });
        return true;
    }

}
